package br.com.cadastro.entidade;


import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import br.com.cadastro.hibernate.util.HibernateUtil;

public class UsuarioDAO{
	
	private SessionFactory factory;
	
	public UsuarioDAO() throws Exception{
		factory = HibernateUtil.getSessionFactory();
		
	}     
	
	public List<Usuario> listarTodos(){
		Session session = factory.openSession();
        session.beginTransaction();
        @SuppressWarnings("unchecked")
		List<Usuario> result = session.createQuery("from Usuario").list();
        session.getTransaction().commit();
        return result;
	}
	
	public void UsInserir(Usuario us) throws Exception {
		Session session = factory.openSession();
		session.save(us);
		session.flush();
		session.close();
	}
	
	public void UsAlterar(Usuario us) throws Exception {
		Session session = factory.openSession();
		session.update(us);
		session.flush();
		session.close();
	}
	public void UsExcluir(Usuario us) throws Exception {
		Session session = factory.openSession();
		session.delete(us);
		session.flush();
		session.close();
	}
}